<?php

namespace app\controller\web;

use app\service\UserDeviceBindingService;
use app\validate\PageValidate;
use app\validate\UserDeviceBindingValidate;
use share\service\UserBaseService;
use share\utils\Helper;

class UserDeviceBindingController extends WebBaseController{

    public function initValidate(){        
        $this->validate = new UserDeviceBindingValidate();
    }

    /**
     * 列表 搜索
     */
    public function list_action(){
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $input = $this->check();       
        $data = UserDeviceBindingService::getListInfo($input,$page);
        $userBases = UserBaseService::getAll(array_column($data,'uid'));
        $res  = [];
        foreach($data as $item){
            $tmp = $item;
            $tmp['iotId'] = $item['iot_id'];
            $user = Helper::filterOne($userBases,['id' => $item['uid']]);
            $tmp['userId'] = '';
            if($user){
                $tmp['userId'] = $user['user_id'];
            }
           
            $res[] = $tmp;
        }
        $total = UserDeviceBindingService::getListCountInfo($input);
        return self::ok(self::formatPageData($page,$total,$res));
    }

}